package djboom.materials.display.elements {
	import djboom.materials.core.BitmapDataCore;
	import djboom.materials.events.InstructionEvent;

	import com.greensock.TweenLite;
	import com.greensock.easing.Cubic;
	import com.greensock.easing.Quart;

	import org.casalib.display.CasaBitmap;
	import org.casalib.util.StageReference;

	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	/**
	 * .
	 * <br>
	 * 
	 * @author DJ BooM 
	 * @since 09.11.2010
	 * 
	 */

	public class SampleMaterial extends SimpleSmartSprite {

		// ----------------------------------------------------------------------------
		// Константы класса.
		// ----------------------------------------------------------------------------
		
		/**
		 * Парафин.
		 * 
		 */

		private static const WAX:String = "work2.Wax";

		/**
		 * Веревка.
		 * 
		 */

		private static const ROPE:String = "work2.Rope";		private static const ROPE_NODULE:String = "work2.RopeNodule";

		// ----------------------------------------------------------------------------
		// Свойства класса.
		// ----------------------------------------------------------------------------

		private var _bitmap:CasaBitmap;
		private var _rope:CasaBitmap;

		private var _blocked:Boolean = false;
		private var _dragHS:HotSpot;

		private var _materialInWax:Boolean = false;
		private var _wax:CasaBitmap;
		private var _ropeNodule:CasaBitmap;

		// ----------------------------------------------------------------------------
		// Конструктор.
		// ----------------------------------------------------------------------------

		public function SampleMaterial(bd:BitmapData) {
			super();
			
			_bitmap = new CasaBitmap(bd);
			addChild(_bitmap);
		}	 

		// ----------------------------------------------------------------------------
		// Приватные методы класса.
		// ----------------------------------------------------------------------------

		/**
		 * Таскаем объект.
		 * 
		 */

		
		private function setupDragSample(event:MouseEvent):void {
			_dragHS.removeEventListener(MouseEvent.MOUSE_DOWN, setupDragSample);
             
			this.startDrag(false, new Rectangle(0, 271, 950, 389));
            
			_dragHS.addEventListener(MouseEvent.MOUSE_UP, stopDragSprite);
			StageReference.getStage().addEventListener(MouseEvent.MOUSE_UP, stopDragSprite);
            
			this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}

		private function onEnterFrame(event:Event):void {
			if (this.x <= 227 && this.y >= 490) {
				this.startDrag(false, new Rectangle(0, 271, 227, 389));
			} else if (this.x <= 227 && this.y < 490) {
				this.startDrag(false, new Rectangle(0, 271, 950, 389));
			} else if (this.x >= 305 && this.x <= 360 && this.y >= 493) {
				this.startDrag(false, new Rectangle(305, 271, 55, 319));
			} else if (this.x >= 305 && this.x <= 360 && this.y < 493) {
				this.startDrag(false, new Rectangle(0, 271, 950, 389));
			} else if (this.x >= 442 && this.y >= 493) {
				this.startDrag(false, new Rectangle(442, 271, 508, 389));
			} else if (this.x >= 442 && this.y < 493) {
				this.startDrag(false, new Rectangle(0, 271, 950, 389));
			} else if (this.x > 360 && this.x < 442 && this.y < 493) {
				this.startDrag(false, new Rectangle(0, 271, 950, 202));
			} else if (this.x > 228 && this.x < 305 && this.y < 493) {
				this.startDrag(false, new Rectangle(0, 271, 950, 202));
			}
            
            
			if (this.x > 305 && this.x < 360 && this.y > 552) {
				if (!_materialInWax) {
					_materialInWax = true;
                    
					_wax = new CasaBitmap(BitmapDataCore.getBitmapData(WAX));
					addChild(_wax);
                    
					addChild(_rope);				}
			}
		}

		/**
		 * Останавливаем таскание объекта.
		 * 
		 */

		private function stopDragSprite(event:MouseEvent = null):void {
			this.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
            
			_dragHS.stopDrag();
            
			StageReference.getStage().removeEventListener(MouseEvent.MOUSE_UP, stopDragSprite);
			_dragHS.removeEventListener(MouseEvent.MOUSE_UP, stopDragSprite);
            
			TweenLite.to(this, 0.4, {x: 102, y: 441, ease: Cubic.easeInOut});
            
			if (_materialInWax) {
				_dragHS.destroy();
				_dragHS = null;
				
				dispatchEvent(new InstructionEvent(InstructionEvent.REFRESH));
			} else {
				_dragHS.addEventListener(MouseEvent.MOUSE_DOWN, setupDragSample);	
			}
		}

		// ----------------------------------------------------------------------------
		// Видимые методы класса.
		// ----------------------------------------------------------------------------
	
		// ----------------------------------------------------------------------------
		// Переопределенные методы.
		// ----------------------------------------------------------------------------

		override public function initSprite(event:Event):void {
			super.initSprite(event);
		}

		override public function clearContent(event:Event):void {
			super.clearContent(event);
			
			_bitmap.destroy();
			_bitmap = null;
			
			if (_rope != null) {
				_rope.destroy();
				_rope = null;
			}
			
			if (_ropeNodule != null) {
				_ropeNodule.destroy();
				_ropeNodule = null;
			}
		}

		// ----------------------------------------------------------------------------
		// Обработчики событий.
		// ----------------------------------------------------------------------------

		// ----------------------------------------------------------------------------
		// Публичные методы.
		// ----------------------------------------------------------------------------

		public function createRope():void {
			_ropeNodule = new CasaBitmap(BitmapDataCore.getBitmapData(ROPE_NODULE));
			_ropeNodule.x = 6;
			_ropeNodule.y = -2;
			addChild(_ropeNodule);
            
			_rope = new CasaBitmap(BitmapDataCore.getBitmapData(ROPE));
			_rope.x = 36;			_rope.y = -129;
			addChild(_rope);
			
			TweenLite.from(_rope, 0.5, {alpha: 0, delay: 1.5, ease: Quart.easeOut});			TweenLite.from(_ropeNodule, 0.5, {alpha: 0, delay: 1.5, ease: Quart.easeOut});
		}

		public function waxMaterial():void {
			_dragHS = new HotSpot();
			_dragHS.drawRect(new Rectangle(0, 0, 30, 30), 0);
			_dragHS.x = 21;
			_dragHS.y = -124;
			addChild(_dragHS);
            
			_dragHS.addEventListener(MouseEvent.MOUSE_DOWN, setupDragSample);
		}

		// ----------------------------------------------------------------------------
		// Методы доступа.
		// ----------------------------------------------------------------------------

		public function get blocked():Boolean {
			return _blocked;
		}

		public function set blocked(blocked:Boolean):void {
			_blocked = blocked;
		}

		public function hideRope():void {
			_rope.visible = false;
			removeChild(_rope);
		}

		public function showRope():void {
			_rope.visible = true;
			addChild(_rope);
		}
	}
}
